package com.piesat.feaedit.feaedit.base.response.handler;

import com.piesat.feaedit.feaedit.base.response.BaseResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.server.ServerWebInputException;

@ControllerAdvice
public class MethodArgumentNotValidExceptionHandler {

    @ResponseBody
    @ExceptionHandler(WebExchangeBindException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object handleValidException(WebExchangeBindException e) {
        return BaseResponse.failure(400, e.getBindingResult().getFieldError().getDefaultMessage());
    }


    @ResponseBody
    @ExceptionHandler(ServerWebInputException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object handleValidException(ServerWebInputException e) {
        return BaseResponse.failure(400, e.getMethodParameter().getParameterName() + " 参数不能为空");
    }
}
